#!/bin/bash
#script name: install_mysql8.sh
###########description##########
#此脚本用于一键安装mysql5.8数据库,此脚本将mysql数据库安装于执行脚本的目录下
#数据库安装前需要将mysql-5.8*.tar.gz文件拷贝至安装脚本路径下
#数据库下载地址: https://downloads.mysql.com/archives/community/
#微信: LarkMidTable2021
#关注公众号 LarkMidTable ，回复 安装包，下载对应的安装包
###########description##########

# MySQL 安装日志信息
log=/var/log/mysql_install.log
# MySQL压缩包路径
MySQLPath=/home/larkmidtable/mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz
# sql 文件
sqlfile=/home/larkmidtable/test.sql

# 卸载系统自带的MARIADB
#rpm -qa|grep mariadb |xargs yum remove -y > /dev/null

# 解压MYSQL安装包
echo '===================================='
echo 'Unzip....'
tar -xJvf $MySQLPath -C /usr/local/
mv /usr/local/mysql-8.0.32-linux-glibc2.12-x86_64 /usr/local/mysql
if [ $? -ne 0 ];then
echo 'MySQL unzip failed!!!'
exit
else
echo '===================================='
echo 'Unpack the success!'
fi

# 创建MYSQL用户和用户组
groupadd mysql
useradd -g mysql mysql
mkdir /usr/local/mysql/data
chown -R mysql:mysql /usr/local/mysql
echo '===================================='
echo 'User created successfully!'

# MYSQL的配置文件
cat > /etc/my.cnf << EOF
[mysql]
default-character-set=utf8
socket=/var/lib/mysql/mysql.sock
[mysqld]
skip-name-resolve
port = 3306
socket=/var/lib/mysql/mysql.sock
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
max_connections=200
character-set-server=utf8
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
EOF
if [ $? -ne 0 ];then
echo 'Failed to write the my.cnf configuration file!!!'
exit
else
echo '===================================='
echo 'my.cnf configuration success!'
echo '===================================='
fi

# 创建 /var/lib/mysql目录，并修改权限
mkdir /var/lib/mysql
chmod 777 /var/lib/mysql

# 开始安装 MYSQL
cd /usr/local/mysql
./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data 2>&1 | tee $log

# 获取 home/larkmidtable 初始密码
InitialPassword=`tail -1 $log |awk '{print $NF}'`

# 复制启动脚本到资源目录，并修改 basedir 和 datadir 为对应目录
cp ./support-files/mysql.server /etc/init.d/mysqld
sed -i '46s/basedir=/basedir=\/usr\/local\/mysql/g' /etc/init.d/mysqld
sed -i '47s/datadir=/datadir=\/usr\/local\/mysql\/data/g' /etc/init.d/mysqld
if [ $? -ne 0 ];then
echo 'Failed to replace /etc/init.d/mysqld!!!'
exit
else
echo '===================================='
echo '/etc/init.d/mysqld successfully modified!'
echo '===================================='
fi

# 设置MYSQL系统服务并开启
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld

# 启动MYSQLD
service mysqld restart

# 检查 MySQL 服务是否启动成功
netstat -ntlp | grep 3306
if [ $? -ne 0 ];then
echo 'MySQL Boot failure!!!'
exit
else
echo '===================================='
echo 'start MySQL successfully!'
fi

# 将 MYSQL 的 BIN目录加到 PATH 环境变量
echo "export PATH=\$PATH:/usr/local/mysql/bin" >> ~/.bash_profile
source ~/.bash_profile

if [ $? -ne 0 ];then
echo 'Variable write failed!!!'
exit
else
echo '===================================='
echo 'Variable write successful!'
echo '===================================='
fi

# 登陆MYSQL，并执行 SQL 语句
#mysql -uroot -p"$InitialPassword" --connect-expired-password < $sqlfile 2> /dev/null
mysql -uroot -p"$InitialPassword" --connect-expired-password -e 'alter user user() identified by "123456";use mysql;update user set user.Host="%" where user.User="root";flush privileges;' 2> /dev/null

if [ $? -ne 0 ];then
echo 'fail to perform MySQL operation!!!'
exit
else
echo '===================================='
echo 'All operations were successful and can be connected through Navicat!!!'
echo '===================================='
fi
#缺少依赖lib.5，建立软链接
echo '缺少依赖lib.5，建立软链接...'
ln -s /usr/lib64/libtinfo.so.6 /usr/lib64/libtinfo.so.5

#重启mysql
echo '重启mysql服务'
systemctl restart  mysqld


